<template>
  <div class="content-box" ref="contentRef">
    <div class="card" ref="infoRef">
      <div class="header">
        <h4 class="title">基本信息</h4>
        <div class="operation">
          <el-button
            type="primary"
            link
            :icon="EditPen"
            :disabled="false"
            v-auth="'/user/changeAdmin'"
            @click="editAdminInfo"
            >编辑</el-button
          >
        </div>
      </div>
      <div class="card-body">
        <el-form
            ref="ruleFormRef"
            label-width="100px"
            label-suffix=" :"
            :rules="rules"
            :model="info"
            :hide-required-asterisk="isView"
        >
          <el-form-item label="账户名" prop="username">
            <el-input :disabled="isView" v-model="info.username" placeholder="请输入账号名称" clearable></el-input>
          </el-form-item>
          <el-form-item label="手机" prop="phone">
            <el-input :disabled="isView" v-model="info.phone" placeholder="请输入手机号" clearable></el-input>
          </el-form-item>
          <el-form-item label="角色">
            <el-tag class="ml-5">{{ info.role.name }}</el-tag>
          </el-form-item>
          <el-form-item label="头像" prop="avatar">
            <UploadImg :disabled="isView" v-model:imageUrl="info.avatar" width="80px" height="80px" :file-size="3">
              <template #empty>
                <el-icon><Avatar /></el-icon>
                <span>请上传头像</span>
              </template>
              <template #tip> 头像大小不能超过 3M </template>
            </UploadImg>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-radio-group :disabled="isView" v-model="info.status">
              <el-radio :value="1">启用</el-radio>
              <el-radio :value="0">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <div class="footer flx-center" v-if="!isView">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
export default {
  name: "sysUserInfo",
}
</script>

<script setup lang="ts">
import { EditPen, Avatar } from "@element-plus/icons-vue";
import UploadImg from "@/components/Upload/Img.vue";
import { onMounted, reactive, ref } from "vue";
import { authStore } from "@/stores/modules/auth";
import { editSysUserApi } from "@/api/modules/sys_user";
import { ElMessage, FormInstance } from "element-plus";
import {SysUserInfo} from "@/interface/api";

const authState = authStore();

const info = ref<any>({ ...authState.user });
const isView = ref(true);

const rules = reactive({
  username: [{ required: true, message: "请输入账号名称" }],
  phone: [{ required: true, message: "请输入手机号" }]
});

const contentRef = ref();
const infoRef = ref();

onMounted(() => {
  infoRef.value.style.height = contentRef.value.offsetHeight + "px";
});

const editAdminInfo = () => {
  isView.value = false;
};

const cancel = () => {
  isView.value = true;
  info.value = { ...authState.user };
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      const res = await editSysUserApi(info.value);
      const userInfo = { ...authState.user } as SysUserInfo;
      userInfo.username = res.data.username;
      userInfo.phone = res.data.phone;
      userInfo.avatar = res.data.avatar;
      authState.setUser(userInfo);
      isView.value = true;
      ElMessage.success({ message: `编辑公司成功！` });
    } catch (error) {
      console.log(error);
    }
  });
};
</script>

<style scoped lang="scss">
:deep(.el-form) {
  width: 100%;
}
.card {
  width: 100%;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 18px;
  }
  .footer {
    line-height: 200px;
    height: 200px;
  }
}
</style>
